<template>
  <div class="login">
    <div class="content">
      <el-tabs type="card">
        <el-tab-pane label="账号密码登录">
          <el-form ref="form" :model="formData" label-width="100px" :rules="rules">
            <el-form-item label="用户名" prop="username">
              <el-input placeholder="请输入用户名" v-model="formData.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input placeholder="请输入密码" v-model="formData.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button style="width: 100%;" type="primary" @click="login">登录</el-button>
            </el-form-item>
            <el-form-item>
              <el-button style="width: 100%;" type="info">重置</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="手机号登录">
          2
        </el-tab-pane>
        <el-tab-pane label="扫码登录">
          3
        </el-tab-pane>
      </el-tabs>

    </div>
  </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref } from 'vue';
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';
const store = useStore()
const router = useRouter()
const form = ref()
const formData = ref({
  username: '',
  password: ''
})
const rules = {
  username: [{ required: true, message: '用户名不可以为空！！', trigger: 'blur' }],
  password: [{ required: true, message: '密码不可以为空！！', trigger: 'blur' }],
}

const login = () => {
  form.value.validate()
    .then(async () => {
      const resp = await axios.post('/pc/login', formData.value)
      const { code, data, msg } = resp.data;
      if (code === 200) {
        ElMessage({ type: 'success', message: msg });
        // 存储用户信息 token
        store.commit('user/setUserInfo', data)

        // 跳转路由
        router.push('/')
      } else {
        ElMessage({ type: 'error', message: msg })
      }
    })
    .catch(() => ({}))
}

</script>

<style lang="scss">
.login {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;

  .content {
    width: 500px;
  }
}
</style>